package com.dense.kuiniu.bookborrow.xjSetting.view.other;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.xjSetting.config.ShareKeyConfig;
import com.dense.kuiniu.bookborrow.xjSetting.listener.XJOnClickListener;
import com.dense.kuiniu.bookborrow.xjSetting.utils.DensityUtil;
import com.dense.kuiniu.bookborrow.xjSetting.utils.PasswordUtils;
import com.dense.kuiniu.bookborrow.xjSetting.utils.SharePreferceUtil;
import com.hoho.android.usbserial.driver.UsbSerialDriver;

import java.util.List;


public class PasswordSettingLayout extends LinearLayout {
    public PasswordSettingLayout(Context context) {
        this(context,null);
    }

    public PasswordSettingLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PasswordSettingLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private View source_input_layout;
    private EditText source;
    private EditText new1;
    private EditText new2;

    private TextView save;


    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.password_layout,this);

        source_input_layout=findViewById(R.id.source_input_layout);

        source=findViewById(R.id.source);
        new1=findViewById(R.id.new1);
        new2=findViewById(R.id.new2);

        save=findViewById(R.id.pass_save);

        save.setOnClickListener(onClickListener);

        String password= SharePreferceUtil.getInstance(getContext()).getString(ShareKeyConfig.DEVICE_PASSWORD,"");
        if (StringUtils.isEmpty(password)){
            source_input_layout.setVisibility(GONE);
        }
    }


    private XJOnClickListener onClickListener=new XJOnClickListener() {
        @Override
        public void onclickView(View view) {
            switch (view.getId()){
                case R.id.pass_save:
                    String password=SharePreferceUtil.getInstance(getContext()).getString(ShareKeyConfig.DEVICE_PASSWORD,"");
                    String sou=source.getText().toString();
                    String n1=new1.getText().toString();
                    String n2=new2.getText().toString();

                    if (StringUtils.isEmpty(n1)||StringUtils.isEmpty(n2)){
                        ToastUtils.showShort("请输入密码");
                    }else if (!n1.equals(n2)){
                        ToastUtils.showShort("密码输入不一致");
                    }else {
                        if (StringUtils.isEmpty(password)) {
                            SharePreferceUtil.getInstance(getContext()).setCache(ShareKeyConfig.DEVICE_PASSWORD, n1);
                            ToastUtils.showShort("密码保存成功");
                            findViewById(R.id.source_input_layout).setVisibility(VISIBLE);
                        } else {
                            if (sou.equals(password)||sou.equals(PasswordUtils.getPassword())) {
                                SharePreferceUtil.getInstance(getContext()).setCache(ShareKeyConfig.DEVICE_PASSWORD, n1);
                                ToastUtils.showShort("密码修改成功");
                            } else {
                                ToastUtils.showShort("原始密码错误");
                            }

                        }
                    }

                    break;
            }
        }
    };


}
